<template>
  <div>
    <div class="userinfo" @click="bindViewTap">
      <!-- <img
        class="userinfo-avatar"
        v-if="userInfo.avatarUrl"
        :src="userInfo.avatarUrl"
        background-size="cover"
      />
      <img
        class="userinfo-avatar"
        src="/static/images/user.png"
        background-size="cover"
      /> -->

      <div class="userinfo-nickname">
        <card :text="userInfo.nickName"></card>
      </div>
    </div>

    <div class="usermotto">
      <div class="user-motto">
        <card :text="motto"></card>
      </div>
    </div>

    <form class="form-container">
      <textarea
        maxlength="-1"
        type="text"
        class="form-control"
        v-model="userInfo.t1"
        placeholder="请输入分析URL"
      />
    </form>

    <button class="all" @click="clickHandle">开始分析</button>
    <div>
      <ul>
        <li v-for="item in logList" :key="item.time">
          <span class="text">{{ item }}</span>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import card from "@/components/card";
import storage from "@/platforms/wechat/storage";
export default {
  data() {
    return {
      motto: "请输入分析URL",
      userInfo: {
        nickName: "UID",
        avatarUrl: "",
        t1: "",
      },
      cardList: [],
      cardList100: [],
      cardList200: [],
      cardList301: [],
      cardList302: [],
      logList: [],
      qs: {
        authkey_ver: "1",
        sign_type: "2",
        auth_appid: "webview_gacha",
        init_type: "301",
        gacha_id: "13d0c223ab4131d488fae6508cd2c564c871b2",
        timestamp: "1637711098",
        lang: "zh-cn",
        device_type: "mobile",
        ext: '{"loc":{"x":156.6002197265625,"y":189.99407958984376,"z":1320.7894287109375},"platform":"Android"}',
        game_version: "CNRELAndroid2.3.0_R5134813_S5082883_D5278928",
        plat_type: "android",
        region: "cn_gf01",
        authkey: "",
        game_biz: "hk4e_cn",
      },
      end_id: 0,
      page: 1,

      start: false,
    };
  },

  components: {
    card,
  },

  methods: {
    bindViewTap() {
      const url = "../logs/main";
      if (mpvuePlatform === "wx") {
        mpvue.switchTab({ url });
      } else {
        mpvue.navigateTo({ url });
      }
    },
    clickHandle(ev) {
      try {

      this.cardList = [];
      this.cardList100 = [];
      this.cardList200 = [];
      this.cardList301 = [];
      this.cardList302 = [];
      this.logList = [];

        this.logList.push("开始获取数据");
        //console.log("this.logList", this.logList);
        //初始化页码
        this.end_id = 0;
        this.page = 1;
        this.start = true;
        //console.log("t1:", this.userInfo.t1);

        var url = this.userInfo.t1;
        var urlsplit = url.split("?");
        if (urlsplit.length < 2) {
          this.logList.push("输入的URL格式不正确");
          return;
        }

        var q1 = urlsplit[1];
        q1 = q1.split("#")[0];

        //console.log("q11", q1);

        this.getUrl(q1);

        let url = "";
        for (let i in this.qs) {
          url += "&" + i + "=" + this.qs[i];
        }
        q1 = url.substr(1);
        //console.log("q12", q1);

        storage.set("newUid", "");
        this.getGachaLog(q1, 100);
      } catch (e) {
        console.log("发生异常", e);
        this.logList.push("发生异常" + e);
      }

      //console.log("完成");
      //console.log("this.end_id:", this.end_id);
      //console.log("this.page:", this.page);

      // //console.log("clickHandle:", ev);
      // throw {message: 'custom test'}
    }, // 从url中获取参数名为name的参数值
    getQueryString(url, name, old) {
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
      var r = url.substr(1).match(reg);
      if (r != null) {
        return unescape(r[2]);
      }
      return old;
    },
    getUrl(url) {
      //this.qs.authkey_ver = this.getQueryString(url, "authkey_ver");
      //console.log("authkey_ver", this.getQueryString(url, "authkey_ver"));

      this.qs.sign_type = this.getQueryString(
        url,
        "sign_type",
        this.qs.init_type
      );
      this.qs.auth_appid = this.getQueryString(
        url,
        "auth_appid",
        this.qs.auth_appid
      );
      this.qs.init_type = this.getQueryString(
        url,
        "init_type",
        this.qs.init_type
      );
      this.qs.gacha_id = this.getQueryString(url, "gacha_id", this.qs.gacha_id);
      this.qs.timestamp = this.getQueryString(
        url,
        "timestamp",
        this.qs.timestamp
      );
      this.qs.lang = this.getQueryString(url, "lang", this.qs.lang);
      this.qs.device_type = this.getQueryString(
        url,
        "device_type",
        this.qs.device_type
      );
      this.qs.ext = encodeURIComponent(this.getQueryString(url, "ext")).replace(
        /%\w\w/g,
        (match) => match.toLowerCase()
      );
      this.qs.game_version = this.getQueryString(
        url,
        "game_version",
        this.qs.game_version
      );
      this.qs.plat_type = this.getQueryString(
        url,
        "plat_type",
        this.qs.plat_type
      );
      this.qs.region = this.getQueryString(url, "region", this.qs.region);

      this.qs.authkey = encodeURIComponent(
        this.getQueryString(url, "authkey")
      ).replace(/%\w\w/g, (match) => match.toLowerCase());
      this.qs.game_biz = this.getQueryString(url, "game_biz", this.qs.game_biz);
    },
    getGachaLog(q1, gacha_type) {
      var q =
        q1 +
        "&gacha_type=" +
        gacha_type +
        "&page=" +
        this.page +
        "&size=18&end_id=" +
        this.end_id;
      this.$axios.get("home/getGachaLog?" + q, {}).then((res) => {
        if (res) {
          if (res.retcode != 0) {
            this.logList.push("获取数据异常:" + res.message);
            return;
          }

          var list = res.data.list;
          //console.log(list);
          var l = list.length;

          //this.logList.push("gacha_type:" + gacha_type + " l:" + l);
          //console.log("this.logList", this.logList);
          //console.log("l:", l);
          if (l === 0) {
            if (gacha_type === 100) {
              if (this.cardList100.length > 0) {
                var uid = this.cardList100[0].uid;
                storage.set("cardList100:" + uid, this.cardList100);
              }
              this.logList.push(
                "新手许愿数据获取完成[" + this.cardList100.length + "]"
              );
              this.end_id = 0;
              this.page = 1;
              this.getGachaLog(q1, 200);
            }
            if (gacha_type === 200) {
              if (this.cardList200.length > 0) {
                var uid = this.cardList200[0].uid;
                storage.set("cardList200:" + uid, this.cardList200);
              }
              this.logList.push(
                "常驻许愿数据获取完成[" + this.cardList200.length + "]"
              );
              this.end_id = 0;
              this.page = 1;
              this.getGachaLog(q1, 301);
            }
            if (gacha_type === 301) {
              if (this.cardList301.length > 0) {
                var uid = this.cardList301[0].uid;
                storage.set("cardList301:" + uid, this.cardList301);
              }
              this.logList.push(
                "角色UP许愿数据获取完成[" + this.cardList301.length + "]"
              );
              this.end_id = 0;
              this.page = 1;
              this.getGachaLog(q1, 302);
            }
            if (gacha_type === 302) {
              if (this.cardList302.length > 0) {
                var uid = this.cardList302[0].uid;
                storage.set("cardList302:" + uid, this.cardList302);
              }
              //console.log("this.cardList302.length:", this.cardList302.length);
              this.logList.push(
                "武器UP许愿数据获取完成[" + this.cardList302.length + "]"
              );
              this.end_id = 0;
              this.page = 1;
              if (this.cardList.length > 0) {
                var uid = this.cardList[0].uid;
                storage.set("cardList:" + uid, this.cardList);
                this.logList.push(
                  "全部数据获取完成[" + this.cardList.length + "]"
                );
                storage.set("newUid", uid);
                setTimeout(() => {
                  this.bindViewTap();
                }, 1000);
              }
            }
            //console.log("return false");

            return;
          }
          if (gacha_type === 100 && this.end_id == 0) {
            this.cardList = list;
          } else {
            this.cardList = this.cardList.concat(list);
          }
          if (this.end_id == 0) {
            if (gacha_type === 100) {
              this.cardList100 = list;
            }
            if (gacha_type === 200) {
              this.cardList200 = list;
            }
            if (gacha_type === 301) {
              this.cardList301 = list;
            }
            if (gacha_type === 302) {
              this.cardList302 = list;
            }
          } else {
            if (gacha_type === 100) {
              this.cardList100 = this.cardList100.concat(list);
            }
            if (gacha_type === 200) {
              this.cardList200 = this.cardList200.concat(list);
            }
            if (gacha_type === 301) {
              this.cardList301 = this.cardList301.concat(list);
            }
            if (gacha_type === 302) {
              this.cardList302 = this.cardList302.concat(list);
            }
          }

          var uid = list[0].uid;
          //console.log("uid:", uid);
          this.userInfo.nickName = uid;

          var uids = storage.get("uids") || [];

          uids.unshift(uid);
          uids = [...new Set(uids)];
          storage.set("uids", uids);
          //console.log("uids", uids);
          var maxid = list[l - 1].id;
          //console.log("maxid:", maxid);

          this.end_id = maxid;
          this.page = this.page + 1;
          setTimeout(() => {
            this.getGachaLog(q1, gacha_type);
          }, 300);

          //console.log("return true");

          return;
        }
      });
    },
  },

  created() {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ["shareAppMessage", "shareTimeline"],
    });
    // let app = getApp()
  },
  onShareAppMessage() {
    return {
      title: "小烈哥原神抽卡分析",
    };
  },
};
</script>

<style scoped>
.userinfo {
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: 9pt;
}

.userinfo-avatar {
  width: 128rpx;
  height: 128rpx;
  margin: 20rpx;
  border-radius: 50%;
  font-size: 9pt;
}

.userinfo-nickname {
  color: #aaa;
  font-size: 9pt;
}

.usermotto {
  margin-top: 15px;
  font-size: 9pt;
}

.form-control {
  display: block;
  padding: 0 12px;
  margin-bottom: 5px;
  border: 1px solid #ccc;
  width: 92%;
  height: 220px;
  font-size: 9pt;
}
.all {
  width: 7.5rem;

  background-color: rgb(238, 255, 0);
  font-size: 9pt;
}
.all:after {
  display: block;
  content: "";
  clear: both;
}
.left {
  float: left;
  width: 3rem;
  height: 1rem;
  background-color: red;
  font-size: 9pt;
}
.text {
  font-size: 9pt;
  color: red;
}
.right {
  float: left;
  width: 4.5rem;
  height: 1rem;
  background-color: green;
  font-size: 9pt;
}
</style>
